package org.brisling.sysman.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.StringUtil;
import org.brisling.sysman.domain.UploadFiles;
import org.brisling.sysman.domain.VersionDomain;
import org.brisling.sysman.queryService.QueryUploadService;
import org.brisling.sysman.repository.VersionRepo;
import org.brisling.sysman.service.base.BaseSysmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/version")
/**
 * 用戶后台服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
public class VersionService extends BaseSysmanService<VersionDomain>{
	
	@SuppressWarnings("unused")
	@Autowired
	private VersionRepo baseRepo;
	
	@Autowired
	private UploadService 	uploadService;
	
	@Autowired
	private QueryUploadService 	qryUploadService;
	
	public VersionService(){
		this.moduleName = ConstSysParam.MODULE_VERSION;
		this.clazz = VersionDomain.class;
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<VersionDomain> getRoleListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<VersionDomain> getRoleList(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 根据角色名称查找角色集合
	 * @param roleNames
	 * @return
	 */
	public List<VersionDomain> getRoleListByRoleNames(List<String> roleNames){
		
		return super.getListByNames(roleNames);
			
	}
	
	/**
	 * 根据角色名称查找角色编码
	 * @param roleNames
	 * @return
	 */
	public List<String> getRoleIdsByRoleNames(List<String> roleNames){
		
		List<VersionDomain> VersionDomain = this.getRoleListByRoleNames(roleNames);
		
		
		return StringUtil.getStringIdList(VersionDomain);
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 保存业务记录
	 * @param id
	 */
	@RequestMapping("/save.action")
	public VersionDomain save(@RequestBody String body){
		VersionDomain beforeSavedDomain = bodyToObj(body, clazz);	
		if(beforeSavedDomain!=null){
			if(beforeSavedDomain.getBizno()==null || beforeSavedDomain.getBizno().trim().length()<=0){
				beforeSavedDomain.setBizno(super.generateBizno());
			}
		}
		
		//执行保存
		VersionDomain savedDomain= null ;
		
		//根据id值判断是否新增操作
		if(beforeSavedDomain.getId()==null){			
			savedDomain= super.save(beforeSavedDomain);
			//更新附件，将临时id(tmpid)替换为保存后的id值
			uploadService.updateTmpid(beforeSavedDomain.getBizcode(),"'"+beforeSavedDomain.getTmpid()+"'",savedDomain.getId().toString());				
			
		}else{		
			savedDomain= super.save(beforeSavedDomain);
		}
		
		return savedDomain;
	}
	
	/**
	 * 获取附件id
	 * @param id	业务记录id
	 * @return
	 */
	@RequestMapping("/getArchiveId.action")
	public Long getArchiveId(@RequestParam(value="id",required=false) Long id){
		
		if(id!=null){
			VersionDomain domain = this.getOne(id);
			
			if(domain!=null){
				Map<String,String> map = new HashMap<String,String>();
				map.put("bizno", domain.getId().toString());
				map.put("bizcode", domain.getBizcode());
				List<UploadFiles> lists = qryUploadService.getList(map);
				if(lists!=null && lists.size()==1){
					return lists.get(0).getId();
				}
			}
		}
		return null;
	}
}
